<?php
// +----------------------------------------------------------------------
// | 活动页面
// +----------------------------------------------------------------------

namespace Home\Controller;
use Common\Controller\Base;
use Common\Model\Model;

class ActivityController extends Base {

    //列表页面
    public function index(){
        $newsModel=M('Activity');
        $where['status']=array('EQ',true);
        $count = $newsModel->where($where)->count();
        $page = $this->Pages($count, 10);
        $pageurl='?page='.urlencode('[PAGE]');
        $page->url     = __APP__.'/activity/'.$pageurl;

        $field=array('title','id','summary','create_time','image');
        $list=$newsModel->field($field)->where($where)->order('sort DESC, id DESC')->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign("list", $list);
        $this->assign("Page", $page->show());
        $this->display();
    }

    //详情页面
    public function read(){

        $infoid=I('get.id');
        if(empty($infoid)){
            $this->_empty();
        }
        M('Activity')->where(array('status'=>true,'id'=>$infoid))->setInc('clicknum',1);
        $data=M('Activity')->where(array('status'=>true,'id'=>$infoid))->find();
        $this->assign('data',$data);

        //评论
        $where['infoid']=$infoid;
        $where['type']=6;
        $where['parent_id'] = 0;
        $count = M('UserMessage')->where($where)->count();

     //   $limitRows = 10; // 设置每页记录数
        $limitRows = C('YF_PAGE');
        $page = $this->ajaxpage($count,$limitRows,'type=msg','ajax_div','ajax_page_div');

        $msgModel=new Model();
        $message=$msgModel
            ->table(array(
                    C('DB_PREFIX').'user_message'=>'M',
                    C('DB_PREFIX').'user'=>'U',
                )
            )
            ->where('M.userid=U.userid and M.status=true and M.infoid='.$infoid.' and M.type=6 and M.parent_id=0')
            ->field('M.create_time,M.content,U.nickname,U.uavatar,U.openuid,U.uprovince,U.ucity,U.userid,M.id,M.touserid')
            ->order('M.create_time DESC')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->select();

//         foreach($message as $k=>$v){
//             $message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
//         }
            
            foreach($message as $k=>$v)
            {
            	$child_temp = array();
            	$child_temp = $msgModel->table(C('DB_PREFIX').'user_message')->field('*')->where(array('parent_id' => $v['id']))->order('create_time ASC')->select();
            
            	$message[$k]['childlist'] = array();
            	if($child_temp)
            	{
            		foreach($child_temp as $yk => $yv)
            		{
            			if($yv['userid'] == 0)
            			{
            				$child_temp[$yk]['nickname'] = "烘焙圈";
            				$child_temp[$yk]['openuid'] = 0;
            			}
            			else
            			{
            				$yf_user = array();
            				$yf_user = M('User')->field('openuid, nickname, uprovince, ucity, uavatar, ifrz')->where(array('userid'=>$yv['userid']))->find();
            				$child_temp[$yk]['nickname'] = $yf_user['nickname'];
            				$child_temp[$yk]['openuid'] = $yf_user['openuid'];
            				$child_temp[$yk]['uprovince'] = $yf_user['uprovince'];
            				$child_temp[$yk]['ucity'] = $yf_user['ucity'];
            				$child_temp[$yk]['uavatar'] = $yf_user['uavatar'];
            				$child_temp[$yk]['ifrz'] = $yf_user['ifrz'];
            			}
            			if($yv['touserid'] != 0)
            			{
            				$child_temp[$yk]['touser'] = M('User')->field('openuid,nickname')->where(array('userid'=>$yv['touserid']))->find();
            			}
            			else
            			{
            				$child_temp[$yk]['touser']['nickname'] = "烘焙圈";
            				$child_temp[$yk]['touser']['openuid'] = 0;
            			}
            		}
            		$message[$k]['childlist'] = $child_temp;
            	}
            	$message[$k]['tempname'] = hy_substr($v['nickname'], 4, true);
            	//$message[$k]['tempname'] = $v['nickname'];
            	$message[$k]['touser'] = array();
            	if(!empty($v['touserid']))
            	{
            		$message[$k]['touser']=M('User')->field('openuid,nickname')->where(array('userid'=>$v['touserid']))->find();
            	}
            }
            unset($where['parent_id']);
            $this->msgcount=M('UserMessage')->where($where)->count();
        $this->msgtype=6;
        $this->assign('msglist',$message);
        $this->assign('page',$page->show());

        if(IS_AJAX){
            exit($this->fetch('msglist'));
        }

        $this->display();

    }
}